package com.tonyodev.fetch2core;

import ohos.utils.net.Uri;

/**
 * Builder used to create a Fetch File Server url.
 *
 * @since 2021-05-20
 */
public class FetchFileServerUriBuilder {
    /**
     * FETCH_URI_SCHEME
     */
    public static final String FETCH_URI_SCHEME = "fetchlocal";
    private String host = "00:00:00:00";
    private int port;
    private String identifier = "";

    /**
     * 返回 host
     *
     * @return host
     */
    public String getHost() {
        return host;
    }

    /**
     * 设置地址
     *
     * @param host 地址
     */
    public void setHost(String host) {
        this.host = host;
    }

    /**
     * 获取端口
     *
     * @return 端口
     */
    public int getPort() {
        return port;
    }

    /**
     * 设置端口
     *
     * @param port 端口
     */
    public void setPort(int port) {
        this.port = port;
    }

    /**
     * 获取identifier
     *
     * @return identifier
     */
    public String getIdentifier() {
        return identifier;
    }

    /**
     * 设置identifier
     *
     * @param identifier identifier
     */
    public void setIdentifier(String identifier) {
        this.identifier = identifier;
    }

    /**
     * Set the IP address of the fetch file server
     *
     * @param hostAddress ip address
     * @return builder
     */
    public FetchFileServerUriBuilder setHostAddress(String hostAddress) {
        this.host = hostAddress;
        return this;
    }

    /**
     * Set the port of the fetch file server
     *
     * @param port port
     * @return builder
     */
    public FetchFileServerUriBuilder setHostPort(int port) {
        this.port = port;
        return this;
    }

    /**
     * Set the IP address and port of the fetch file server
     *
     * @param hostAddress ip address
     * @param port port
     * @return builder
     */
    public FetchFileServerUriBuilder setHostInetAddress(String hostAddress, int port) {
        this.port = port;
        this.host = hostAddress;
        return this;
    }

    /**
     * Set the file resource identifier. This could be the content file id or content file name.
     *
     * @param fileResourceName resource identifier
     * @return builder
     */
    public FetchFileServerUriBuilder setFileResourceIdentifier(String fileResourceName) {
        this.identifier = fileResourceName;
        return this;
    }

    /**
     * Set the file resource id identifier.
     *
     * @param fileResourceId resource id identifier
     * @return builder
     */
    public FetchFileServerUriBuilder setFileResourceIdentifier(long fileResourceId) {
        this.identifier = String.valueOf(fileResourceId);
        return this;
    }

    /**
     * Create Fetch file server URI.
     *
     * @return uri
     */
    public Uri build() {
        return new Uri.Builder()
            .scheme(FETCH_URI_SCHEME)
            .encodedAuthority(this.host + ':' + this.port)
            .appendDecodedPath(this.identifier)
            .build();
    }

    @Override
    public String toString() {
        return build().toString();
    }
}
